import csv
from collections import namedtuple

# 想读写一个csv格式的文件
def demo_1():
    with open('../temp/demo_csv.csv') as f:
        f_csv = csv.reader(f)
        headings = next(f_csv)
        # 使用命名元组，进行数据字段命名
        Row = namedtuple('Row', headings)
        for r in f_csv:
            row = Row(*r)
            print(row)

# 写入csv文件
def demo_2():
    headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
    rows = [
        {'Symbol':'DD', 'Price':50.01, 'Date': '7/28/2018', 'Time': '9:20pm', 'Change': -9.4, 'Volume': 181232}
    ]
    with open('../temp/demo_csv.csv', 'w') as f:
        f_csv = csv.DictWriter(f, headers)
        f_csv.writeheader()
        f_csv.writerows(rows)


if __name__ == '__main__':
    # demo_1()
    demo_2()